using System.Collections.Generic;
using System.Drawing;
using System.IO;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class RecordMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.record_Menu, new Point(0, -107));

	private static List<string> nameList;

	private static bool isSave;

	private static bool isMenuInitialized;

	public RecordMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnMenuClose += OnMenuClose;
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	public static void InitRecordMenu(bool _isSave)
	{
		isSave = _isSave;
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		if (isSave)
		{
			menu.AddItem(new UIMenuItem(TextSetting.record_MenuItem_NewFile));
		}
		nameList = new List<string>();
		FileInfo[] files = new DirectoryInfo($"{Skin.SKIN_PATH}").GetFiles("*.skin", SearchOption.AllDirectories);
		foreach (FileInfo fileInfo in files)
		{
			nameList.Add(fileInfo.FullName);
		}
		if (nameList.Count != 0)
		{
			for (int j = 0; j < nameList.Count; j++)
			{
				menu.AddItem(new UIMenuItem(Path.GetFileNameWithoutExtension(nameList[j])));
			}
		}
		menu.RefreshIndex();
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		bool flag = false;
		if (isSave)
		{
			if (index == 0)
			{
				if (DressingScript.Dressing == Main.Member.P2)
				{
					string modelName = Main.P2.skin.ModelName;
					string userInput = Game.GetUserInput($"{modelName}", 30);
					flag = Skin.SkinFileSave(Main.P2, userInput);
				}
				else
				{
					string modelName2 = Main.P1.skin.ModelName;
					string userInput2 = Game.GetUserInput($"{modelName2}", 30);
					flag = Skin.SkinFileSave(Main.P1, userInput2);
				}
			}
			else if (DressingScript.Dressing == Main.Member.P2)
			{
				if (Game.GetUserInput("y/n", 30).ToLower() == "y")
				{
					string userInput3 = Game.GetUserInput($"{Path.GetFileNameWithoutExtension(nameList[index - 1])}", 30);
					flag = Skin.SkinFileSave(Main.P2, userInput3, nameList[index - 1]);
				}
			}
			else if (Game.GetUserInput("y/n", 30).ToLower() == "y")
			{
				string userInput4 = Game.GetUserInput($"{Path.GetFileNameWithoutExtension(nameList[index - 1])}", 30);
				flag = Skin.SkinFileSave(Main.P1, userInput4, nameList[index - 1]);
			}
			if (flag)
			{
				SoundHelper.PlayExternalSound(Resource.SAVE);
			}
			InitRecordMenu(isSave);
		}
		else
		{
			if (DressingScript.Dressing == Main.Member.P2)
			{
				flag = Skin.SkinFileLoad(ref Main.P2, nameList[index]);
			}
			else
			{
				flag = Skin.SkinFileLoad(ref Main.Clone, nameList[index], isClone: true);
				flag = Skin.SkinFileLoad(ref Main.P1, nameList[index]);
			}
			if (flag)
			{
				SoundHelper.PlayExternalSound(Resource.LOAD);
				DressingScript.ReDressing(DressingScript.Dressing);
			}
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
